/**
 * Copyright 2009 Adam Ruggles.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.google.code.sapien.action;

import java.util.List;

import org.apache.struts2.convention.annotation.Results;
import org.apache.struts2.convention.annotation.Result;

import com.google.code.sapien.model.Category;
import com.google.code.sapien.model.Discussion;
import com.google.code.sapien.service.CategoryService;
import com.google.code.sapien.service.DiscussionService;
import com.google.code.sapien.util.PaginatedResult;
import com.google.inject.Inject;
import com.opensymphony.xwork2.ActionSupport;

/**
 * Category Action, controls the view of the index page for a given category.
 * @author Adam
 * @version $Id$
 * 
 * Created on Jun 10, 2009 at 8:52:49 PM 
 */
@Results({
	  @Result(name = "success", location = "index.jsp")
})
public class CategoryAction extends ActionSupport {

	/**
	 * Serial Version UID.
	 */
	private static final long serialVersionUID = -2054248044392239136L;

	/**
	 * A List of categories for displaying in the view.
	 */
	private List<Category> categories;

	/**
	 * The category service.
	 */
	private final CategoryService categoryService;

	/**
	 * The discussion service.
	 */
	private final DiscussionService discussionService;

	/**
	 * The category id to load.
	 */
	private Long id;

	/**
	 * The page number.
	 */
	private int page = 1;

	/**
	 * A PaginatedResult of discussions.
	 */
	private PaginatedResult<Discussion> paginatedResult;

	/**
	 * Constructs a category action.
	 * @param discussionService The discussion service.
	 * @param categoryService The categoryService.
	 */
	@Inject
	public CategoryAction(final DiscussionService discussionService, final CategoryService categoryService) {
		super();
		this.discussionService = discussionService;
		this.categoryService = categoryService;
	}

	/**
	 * Returns a paginated list of discussions for a given category id.
	 * {@inheritDoc}
	 * @see com.opensymphony.xwork2.ActionSupport#execute()
	 */
	@Override
	public String execute() {
		paginatedResult = discussionService.getModifiedDesc(id, page, IndexAction.RESULTS_PER_PAGE);
		categories = categoryService.getAll();
		return SUCCESS;
	}

	/**
	 * Returns categories.
	 * @return the categories.
	 */
	public List<Category> getCategories() {
		return categories;
	}

	/**
	 * Returns id.
	 * @return the id.
	 */
	public Long getId() {
		return id;
	}

	/**
	 * Returns page.
	 * @return the page.
	 */
	public int getPage() {
		return page;
	}

	/**
	 * Returns paginatedResult.
	 * @return the paginatedResult.
	 */
	public PaginatedResult<Discussion> getPaginatedResult() {
		return paginatedResult;
	}

	/**
	 * Sets id.
	 * @param id the id to set.
	 */
	public void setId(final Long id) {
		this.id = id;
	}

	/**
	 * Sets page.
	 * @param page the page to set.
	 */
	public void setPage(final int page) {
		if (page <= 0) {
			this.page = 1;
		} else {
			this.page = page;
		}
	}
}
